﻿function MenuSecure ( TheRole ) {

	if ( TheRole == 'ADMIN' )
	{
         $('#ListMenu_employee').remove();
    }
	else if ( TheRole == 'EMPL' )
	{
	     $('#ListMenu_admin').remove();
	}
	else 
	{
	     $('#ListMenu_employee').remove();
         $('#ListMenu_admin').remove();
    }

};	
	
$("#callCancel").click(function(e){
    e.preventDefault();
    sessionStorage.setItem('theName', '');
	sessionStorage.setItem('thePass', '');
	$.mobile.changePage('#plash');
	history.go(0);
    return false;
});

$(function() {

	 $("#callSignin").click(function() {
		  theName = $.trim($("#theName").val());
		  thePass = $.trim($("#thePass").val());
		 if(theName.length > 0)
		 {
            console.log(URLServiceLogin + '/login.php?name='+theName+'&pass='+thePass);
			$.getJSON(URLServiceLogin + '/login.php?name='+theName+'&pass='+thePass, function(data) {
			
				var info = data.items;
				var len = info.length;
                 
				 if ( len == 1 ) {
                    console.log(info[0].language);
					console.log(info[0].role);
					sessionStorage.setItem('theName', theName);
					sessionStorage.setItem('thePass', thePass);
					localStorage.setItem('theName', theName);
					localStorage.setItem('thePass', thePass);					
					sessionStorage.setItem('theLang', info[0].language);
					sessionStorage.setItem('theRole', info[0].role);
					language_translate_all();
					$.mobile.changePage('#PageMenu');
					history.go(0);
				 }					
				 else {
					sessionStorage.setItem('theName', '');
					$("#resultLog").html("Result: Error signing on. Please retry or Contact your administrator" );
				 }							

			});				 
				 
		 }
	 });

	 $("#resultLog").ajaxError(function(event, request, settings, exception) {
		  if ( request.status != 200)  {
			$("#resultLog").html("Error Calling url : <br />HTTP Code: " + request.status);
		  }
	 });

});


$("#signout_button").click(function(e){
    e.preventDefault();
    sessionStorage.setItem('theName', '');
	sessionStorage.setItem('thePass', '');
	$.mobile.changePage('#plash');
	history.go(0);
    return false;
});

$("#PageMenu").live("pageshow", function (event) {
    MenuSecure ( sessionStorage.getItem('theRole') );
});

$("#popupLogin").live("pageshow", function (event) {
    $("#theName").val(localStorage.getItem('theName'));
    $("#thePass").val(localStorage.getItem('thePass'));		  		
});

